import { ref, Ref } from 'vue'
interface PicType {
  appRef: Ref<HTMLElement>
  calcRate: () => void
  windowDraw: () => void
}
export default function useIndex(): PicType {
  // * 指向最外层容器
  const appRef = ref()
  // * 定时函数
  const timer = ref(0)
  // * 默认缩放值
  const scale = {
    width: '1920',
    height: '810'
  }
  // * 设计稿尺寸（px）5120/2160=2.37037
  const baseWidth = 1920
  const baseHeight = 810
  // * 需保持的比例（默认1.77778）

  // 高度提升比例
  // let hScale = window.innerHeight / baseHeight
  let hScale = 1.333333 * 1.111111
  if (import.meta.env.MODE.indexOf('development') > -1) {
    console.log('开发环境')
    hScale = 1
  } else {
    console.log('生产环境')
  }
  const baseProportion = parseFloat((baseWidth / baseHeight).toFixed(5))
  const calcRate = () => {
    // 当前宽高比
    const currentRate = parseFloat((window.innerWidth / window.innerHeight).toFixed(5))
    if (appRef.value) {
      if (currentRate > baseProportion) {
        // 表示更宽
        // scale.width = ((window.innerHeight * baseProportion) / baseWidth).toFixed(5)
        scale.height = (window.innerHeight / baseHeight).toFixed(5)
        scale.height = ((window.innerHeight / baseHeight) * hScale).toFixed(5)
        appRef.value.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
      } else {
        // 表示更高
        // scale.height = (window.innerWidth / baseProportion / baseHeight).toFixed(5)
        scale.height = ((window.innerWidth / baseProportion / baseHeight) * hScale).toFixed(5)
        scale.width = (window.innerWidth / baseWidth).toFixed(5)
        appRef.value.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
      }
    }
  }

  const resize = () => {
    clearTimeout(timer.value)
    timer.value = window.setTimeout(() => {
      calcRate()
    }, 200)
  }

  // 改变窗口大小重新绘制
  const windowDraw = () => {
    window.addEventListener('resize', resize)
  }

  return {
    appRef,
    calcRate,
    windowDraw
  }
}
